package com.yanqu.road.dao.impl.activity.pawnshop;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.CrossPawnshopTriggerData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossPawnshopTriggerUserDaoImpl extends TempDataObjectDao<CrossPawnshopTriggerData> {
    public static String getCrossPawnshopTriggerDataSql = "select * from `t_c_activity_pawnshop_trigger_user` where `activityId`=?";
    public List<CrossPawnshopTriggerData> getCrossPawnshopTriggerData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossPawnshopTriggerData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossPawnshopTriggerDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossPawnshopTriggerData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossPawnshopTriggerDataSql = "update `t_c_activity_pawnshop_trigger_user` set userId=?,triggerTime=? where activityId=? and id=?";
    @Override
    protected boolean updateData(CrossPawnshopTriggerData crossPawnshopTriggerData) {
        crossPawnshopTriggerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossPawnshopTriggerData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPawnshopTriggerData.getTriggerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPawnshopTriggerData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPawnshopTriggerData.getId()));
        if (getDb().executeNoneQuery(updateCrossPawnshopTriggerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPawnshopTriggerDataSql = "insert into `t_c_activity_pawnshop_trigger_user`(activityId,id,userId,triggerTime) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossPawnshopTriggerData crossPawnshopTriggerData) {
        crossPawnshopTriggerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossPawnshopTriggerData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPawnshopTriggerData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPawnshopTriggerData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPawnshopTriggerData.getTriggerTime()));
        if (getDb().executeNoneQuery(insertCrossPawnshopTriggerDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPawnshopTriggerData parseData(ResultSet rs) throws SQLException {
        CrossPawnshopTriggerData crossPawnshopTriggerData = new CrossPawnshopTriggerData();
        crossPawnshopTriggerData.setActivityId(rs.getInt("activityId"));
        crossPawnshopTriggerData.setId(rs.getInt("id"));
        crossPawnshopTriggerData.setUserId(rs.getLong("userId"));
        crossPawnshopTriggerData.setTriggerTime(rs.getLong("triggerTime"));

        return crossPawnshopTriggerData;
    }

}
